/**
 * 操作面板模块
 * @author none
 */
class ZJHOperatePanelModule extends how.module.Module 
{
    public static response: any = {
        setOperateUIOperatePanel: "setOperateUIOperatePanel",
        autoLead: "autoLead",
        onGameEnd: CMDConfig.GET_GAMEEND,
        onGetLookCard: CMDConfig.GET_LOOKCARDS,//收到看牌
        onRoundOver: CMDConfig.GET_ROUNDOVER,
        onLunci: CMDConfig.GET_POOLTOPLAYER
    }
    public static request: any = {
        onSendGenzhu: "sendGenzhu",
        onSendBipai: "sendBipai",
        onQipai: "qipai",
        onJiazhu: "jiazhu"
    }
    private get gameData(): ZJHGameData {
        return <ZJHGameData>AppData.getInstance().gameData;
    }
    /*
     * 设置可以操作UI
     * */
    public setOperateUIOperatePanel(gameData: ZJHGameData): void 
    {
        console.log(">>> ZJHOperatePanelModule setOperateUIOperatePanel 设置可以操作UI");
        this.gui.visible = true;
        this.callUI("setOperateUI", gameData);
    }
    //收到轮次变化
    public onLunci(data: any): void 
    {
        console.log(">>> ZJHOperatePanelModule onLunci 收到轮次变化");
        this.callUI("setLunciUI", data);
    }
    /**
     * 发送跟注
     */
    public onSendGenzhu(wState: number = 0): void 
    {
        console.log(">>> ZJHOperatePanelModule onSendGenzhu 发送跟注");
        this.communicate(ZJHPlayerModule.response.startorStopClock, AppData.getInstance().userData.chairID, false);
        var score = this.gameData.currentScore;
        score = wState == 1 ? score * 2 : score;
        this.hallSocket.send(CMDConfig.SEND_CALL_CHIP, {
            lScore: score,
            wState: wState
        });
        this.gui.visible = false;
    }
    /**
     * 发送比牌
     */
    public onSendBipai(): void 
    {
        console.log(">>> ZJHOperatePanelModule onSendBipai 发送比牌");
        var playerList = this.gameData.getPlayersByTableIDHaveChairID();
        if (playerList.length == 2) {
            this.communicate(ZJHPlayerModule.response.startorStopClock, AppData.getInstance().userData.chairID, false);
            this.onSendGenzhu(1);
            var targetUser: number;
            for (var i: number = 0; i < playerList.length; i++) {
                if (playerList[i].chairID != AppData.getInstance().userData.chairID) {//如果不是自己，就是比牌对象
                    targetUser = playerList[i].chairID;
                    break;
                }
            }
            this.hallSocket.send(CMDConfig.SEND_COMPARECARD, { wCompareUser: targetUser });
        }
        else {
        }
    }
    /*
     * 倒计时结束自动出牌
     */
    public autoLead(): void 
    {
        console.log(">>> ZJHOperatePanelModule autoLead 倒计时结束自动出牌");
        //停止自己出牌倒计时
        this.onQipai();
    }
    /*
     * 弃牌
     */
    public onQipai(): void 
    {
        console.log(">>> ZJHOperatePanelModule onQipai 弃牌");
        this.communicate(ZJHPlayerModule.response.startorStopClock, AppData.getInstance().userData.chairID, false);
        this.hallSocket.send(CMDConfig.SEND_QIPAi);
        this.callUI("onOperateOver");
    }
    /**
     * 加注
     */
    public onJiazhu(value: number): void 
    {
        console.log(">>> ZJHOperatePanelModule onJiazhu 加注");
        this.communicate(ZJHPlayerModule.response.startorStopClock, AppData.getInstance().userData.chairID, false);
        var score = this.gameData.currentScore + value;
        this.hallSocket.send(CMDConfig.SEND_CALL_CHIP, { lScore: score, wState: 0 });
        this.callUI("onOperateOver");
    }
    /**
     * 游戏结束
     */
    public onGameEnd(data: any): void 
    {
        console.log(">>> ZJHOperatePanelModule onGameEnd 游戏结束");
        this.callUI("onOperateOver");
    }
    /**
     * 收到看牌消息
     */
    public onGetLookCard(data: any): void {
        console.log(">>> ZJHOperatePanelModule onGetLookCard 收到看牌消息");
        if (data.wLookCardUser == AppData.getInstance().userData.chairID) {//如果是自己看的牌
            this.callUI("setLookCardUI", this.gameData);
        }
    }
    public onRoundOver(data: any): void {
        this.callUI("onRoundOver");
    }
}
